package com.hue.gulimailseckill.interceptor;

import com.hue.common.constant.AuthConstant;
import com.hue.common.constant.CartConstant;
import com.hue.common.to.UserInfoTo;
import com.hue.common.vo.UserVo;
import org.apache.catalina.User;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @Author: 姚凤桥
 * @Date: 2021/12/12 21:02
 * @Description: 判断用户登录状态的拦截器
 */
public class LoginStatusInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserVo loginUser = (UserVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);

        if (loginUser != null) {
            threadLocal.set(loginUser);
        }

        AntPathMatcher matcher = new AntPathMatcher();
        if (matcher.match("/kill", request.getRequestURI()) && loginUser == null) {
            response.sendRedirect("http://auth.gulimail.com");
            return false;
        }

        return true;
    }

    /**
     * 在响应请求时，命令浏览器存储一个user-key
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {

    }
}
